package com.sgb.merchant.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.link.entity.merchant.DemandDetailEntity;
import com.sgb.link.entity.merchant.DemandInfoEntity;
import com.sgb.link.entity.merchant.DemandType;
import com.sgb.link.entity.merchant.FileInfo;
import com.sgb.merchant.api.MerchantsNet;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/9/17
 * @Description: 发布投资需求model
 */
public class PublishDemandModel extends BaseViewModel {
    public MutableLiveData<DemandDetailEntity> ldInfo=new MutableLiveData<>();
    public MutableLiveData<Boolean> ldResult=new MutableLiveData<>();
    public MutableLiveData<String> ldUrl=new MutableLiveData<>();
    public MutableLiveData<List<DemandType>> ldType=new MutableLiveData<>();
    public MutableLiveData<String> ldFileInfo=new MutableLiveData<>();
    Gson gson=new Gson();

    public PublishDemandModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void uploadFile(File file) {

        try {
            Map<String, RequestBody> photos = new HashMap<>();
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            String fileName = URLEncoder.encode(file.getName(), "UTF-8");
            photos.put("file" + "\"; filename=\"" + fileName, requestBody);
            MerchantsNet.getInstance().getService().addFile(photos).enqueue(new ICallBack<BaseEntity>() {
                @Override
                public void onSuccess(BaseEntity data) {
                    if (data.isSuccess()) {
                        ldUrl.postValue(data.getData().toString());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity> call, Throwable t) {
                    super.onFailure(call, t);
                   /* ldResult.postValue(false);*/
                }

            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /***
     * 获取需求类型
     */
    public void getType(){
        MerchantsNet.getInstance().getService().getType().enqueue(new ICallBack<BaseEntity<List<DemandType>>>() {
            @Override
            public void onSuccess(BaseEntity<List<DemandType>> data) {
                if(data.isSuccess()){
                    ldType.postValue(data.getData());
                }
            }
        });

    }

    public void getInfo(String no){
        MerchantsNet.getInstance().getService().getDemandsDetail(no).enqueue(new ICallBack<BaseEntity<DemandDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<DemandDetailEntity> data) {
                if (data.getData() == null ) {
                    return;
                }
                ldInfo.postValue(data.getData());
            }
        });
    }
    public void edit(Map entity){
        MerchantsNet.getInstance().getService().editDemands(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    ldResult.postValue(true);
                }else{
                    ldResult.postValue(false);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                super.onFailure(call, t);
                ldResult.postValue(false);
            }
        });
    }
    public void getFileInfo(String url){
        MerchantsNet.getInstance().getService().getFileInfo(url).enqueue(new ICallBack<BaseEntity<FileInfo>>() {
            @Override
            public void onSuccess(BaseEntity<FileInfo> data) {
                if(data.isSuccess()){
                    ldFileInfo.postValue(gson.toJson(data.getData()));
                }
            }
        });
    }
}
